﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace d39
{
    public partial class d39Master : System.Web.UI.MasterPage
    {
        protected GridPage GridPage { get { return Page as GridPage; } }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Makes it possible to function when not connected to the internet - eg when on the train - by putting the jquery file in the Scripts folder
            if (Request.Url.Host == "localhost" && File.Exists(Server.MapPath("~/Scripts/jquery-1.4.2.min.js")))
            {
                LocalJQuery.Visible = true;
                HostedJQuery.Visible = false;
            }
            if (GridPage != null)
            {
                var currentPage = "~" + Request.Url.AbsolutePath;
                foreach (MenuItem item in NavigationMenu.Items)
                {
                    item.Selected = item.NavigateUrl.StartsWith(currentPage) || (currentPage.ToLower() == "~/default.aspx" && item.NavigateUrl == "~/");
                    if (item.NavigateUrl.EndsWith(".aspx"))
                    {
                        // 7 = length of "~/" + ".aspx"
                        item.NavigateUrl = GridPage.CurrentGrid.GetPageUrl(
                            item.NavigateUrl.Substring(2, item.NavigateUrl.Length - ("~/".Length + ".aspx".Length)), UrlTweaks.Default);
                    }
                }
                if (GridPage.CurrentGrid.Name == null)
                {
                    GridName.Visible = false;
                }
                else
                {
                    GridName.Visible = true;
                    GridName.Text = " - " + Server.HtmlEncode(GridPage.CurrentGrid.Name);
                }
            }
        }
    }
}